/**
        Example of using QtConcurrent::mappedReduced
        for integral computation
*/

#include <QtGui/QApplication>
#include <QtConcurrentMap>
#include <QList>
#include <QDebug>

int n = 10000;
double a = 0.0;
double b = 1.0;

double square2(double x) {
    return x*x;
}

static double rectangleS(double x) {
    return square2(x) * (b - a) / n;
}

static void sumN(double& acc, const double & x) {
    acc += x;
}

int main(int argc, char *argv[]) {
    QList<double> points;
    for (int i = 0; i < n; ++i)
        points.push_back((double)i * (b - a) / n);
    QFuture<double> res = QtConcurrent::mappedReduced(points, rectangleS, sumN);
    res.waitForFinished();
    qDebug() << res.result();
    return 0;
}
